<template>
    <div class="text-grid-container">
        <button @click="BeginshowModal">预览</button>
        <teleport to="body">
            <div v-if="showModal" class="modal-mask">
                <div class="modal-wrapper">
                    <div class="modal-container">
                        <div class="modal-header">
                            <h2>文本网格展示</h2>
                            <select v-model="currentLayout" class="layout-select">
                                <option v-for="(layout, index) in layouts" :key="index" :value="layout">
                                    {{ layout.name }} ({{ layout.cols }}×{{ layout.rows }})
                                </option>
                            </select>
                            <button class="close-btn" @click="showModal = false">×</button>
                        </div>

                        <div class="modal-content">
                            <div v-for="(paragraph, pIndex) in processedData" :key="pIndex" class="paragraph-container">
                                <div class="grid-title">段落 {{ pIndex + 1 }}</div>
                                <div class="character-grid" :style="gridStyle">
                                    <div v-for="(charObj, cIndex) in paragraph.chars" :key="cIndex"
                                        class="character-cell"
                                        :class="{ 'over-limit': cIndex >= currentLayout.maxChars }">
                                        {{ charObj.word }}
                                    </div>
                                </div>
                                <div v-if="paragraph.isOverflow" class="overflow-warning">
                                    超出字数限制！（当前布局最多 {{ currentLayout.maxChars }} 字）
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </teleport>
    </div>
</template>

<script setup name="Wordshow" >
import { ref, computed, watch } from 'vue'
import axios from 'axios'

const props = defineProps({
    text: {
        type: String,
        required: true
    }
})
let BeginshowModal= ()=>{
    showModal.value = true
    fetchData()
}


const showModal = ref(false)
const backendData = ref([])
const currentLayout = ref({
    name: '6×2',
    cols: 6,
    rows: 2,
    maxChars: 12
})

const layouts = ref([
    { name: '6×2', cols: 6, rows: 2, maxChars: 12 },
    { name: '6×3', cols: 6, rows: 3, maxChars: 18 },
    { name: '8×2', cols: 8, rows: 2, maxChars: 16 },
    { name: '8×4', cols: 8, rows: 4, maxChars: 32 },
    { name: '9×7', cols: 9, rows: 7, maxChars: 63 }
])

// 获取后端数据
const fetchData = async () => {
    try {
        const response = await axios.get('http://localhost:8080/word/getwordlist', {
           params:{
            content: props.text
           }
        })
        backendData.value = response.data.data
    } catch (error) {
        console.error('请求失败:', error)
        backendData.value = []
    }
}

// 处理后的数据
const processedData = computed(() => {
    return backendData.value.map(paragraph => {
        const chars = paragraph.flatMap(item =>
            item.word === '\r' ? [{ ...item, word: '↵' }] : item
        )
        return {
            chars,
            isOverflow: chars.length > currentLayout.value.maxChars
        }
    })
})

// 网格样式
const gridStyle = computed(() => ({
    gridTemplateColumns: `repeat(${currentLayout.value.cols}, 1fr)`,
    gridTemplateRows: `repeat(${currentLayout.value.rows}, 1fr)`
}))
</script>
<style scoped>
/* 基础容器样式 */
.text-grid-container {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 触发按钮样式 */
button {
  width: 200px;
  height: 40px;
  margin-top: 20px;
  margin-left: 10px;
  background: #e5f2ff;
  color: #409eff;
  border: 1px solid #57abff;
  border-radius: 6px ;

  cursor: pointer;
  transition: all 0.3s ease;
}

button:hover {
  background: #409eff;
  color: #e5f2ff;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 模态框遮罩层 */
.modal-mask {
  position: fixed;
  z-index: 9999;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.3s;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* 模态框容器 */
.modal-container {
  width: 80%;
  max-width: 1000px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

/* 模态框头部 */
.modal-header {
  display: flex;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-bottom: 1px solid #e9ecef;
}
h2{
    width: 300px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.5rem;
  color: #2c3e50;
  flex-grow: 1;
}

/* 布局选择器 */
.layout-select {
  padding: 8px 12px;
  border: 1px solid #ced4da;
  border-radius: 6px;
  margin: 0 15px;
  font-size: 14px;
  background: white;
  transition: border-color 0.3s;
}

.layout-select:focus {
  border-color: #409eff;
  outline: none;
}

/* 关闭按钮 */
.close-btn {
  font-size: 28px;
  color: #6c757d;
  transition: color 0.3s;
  padding: 0 12px;
}

.close-btn:hover {
  color: #409eff;
}

/* 内容区域 */
.modal-content {
  padding: 20px;
  overflow-y: auto;
  flex-grow: 1;
}

/* 段落容器 */
.paragraph-container {
  margin-bottom: 30px;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.paragraph-container:last-child {
  margin-bottom: 0;
}

/* 网格标题 */
.grid-title {
  font-weight: 600;
  color: #495057;
  margin-bottom: 12px;
  font-size: 1.1rem;
}

/* 字符网格 */
.character-grid {
  display: grid;
  gap: 1px;
  background: #dee2e6;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  overflow: hidden;
}

/* 字符单元格 */
.character-cell {
  background: white;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: #2c3e50;
  transition: all 0.2s ease;
  position: relative;
}

/* 换行符特殊样式 */
.character-cell:has(span[data-type="newline"]) {
  background: #f1f3f5;
  color: #868e96;
  font-style: italic;
}

/* 超限样式 */
.character-cell.over-limit {
  background: #ffe3e3;
  color: #ff6b6b;
}



/* 溢出警告 */
.overflow-warning {
  color: #ff6b6b;
  font-size: 0.9rem;
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.overflow-warning::before {
  content: "⚠";
  font-size: 1.2rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-container {
    width: 95%;
  }

  .character-cell {
    min-height: 40px;
    font-size: 16px;
  }

  .modal-header {
    flex-wrap: wrap;
    gap: 10px;
  }

  .layout-select {
    order: 1;
    width: 100%;
    margin: 10px 0;
  }
}

@media (max-width: 480px) {
  .character-cell {
    min-height: 35px;
    font-size: 14px;
  }

  .modal-header h2 {
    font-size: 1.2rem;
  }
}

/* 自定义滚动条 */
.modal-content::-webkit-scrollbar {
  width: 8px;
}

.modal-content::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.modal-content::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}

.modal-content::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>